package com.woniu.register.controller;

import com.woniu.common.util.ResultVO;
import com.woniu.register.dto.OwnerDTO;
import com.woniu.register.entity.Owner;
import com.woniu.register.service.OwnerService;
import com.woniu.register.service.OwnerTransferService;
import com.woniu.register.service.OwnerTypeService;
import com.woniu.register.VO.TypeVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 晨星
 * @date 2022/12/07/19:41
 */
@RestController
@RequestMapping("Ownership")
public class OwnerController {

    private final OwnerService ownerService;
    private final OwnerTypeService ownerTypeService;
    private final OwnerTransferService ownerTransferService;

    public OwnerController(OwnerService ownerService, OwnerTypeService ownerTypeService, OwnerTransferService ownerTransferService) {
        this.ownerService = ownerService;
        this.ownerTypeService = ownerTypeService;
        this.ownerTransferService = ownerTransferService;
    }

    /**
     * 点击下拉框选择类别
     *
     * @author 晨星
     * @date 2022/12/8 17:19
     **/
    @PostMapping("")
    public ResultVO selectType() {
        List<TypeVO> typeList = ownerTypeService.selectType();
        return ResultVO.success("typeList", typeList);
    }

    /**
     * 查询所有权登记配套的文件
     *
     * @param num 1是初始登记，2是转移登记
     * @author 晨星
     * @date 2022/12/12 15:53
     **/
    @PostMapping("selectFile/{num}")
    public ResultVO selectFile(@PathVariable Integer num) {
        List<String> fileList = ownerService.selectFile(num);
        return ResultVO.success("fileList", fileList);
    }

    @PostMapping("insert")
    public ResultVO insertOwner(@RequestBody OwnerDTO owner) {
        if (owner.getTypeId() == 1) {
            int i = ownerService.insertOwner(owner);
            if (i > 0) {
                return ResultVO.success();
            } else {
                return ResultVO.failure();
            }
        }else if (owner.getTypeId() == 2){
            int i = ownerTransferService.insertOwnerTransfer(owner);
            if (i > 0) {
                return ResultVO.success();
            } else {
                return ResultVO.failure();
            }
        }
        else {
            return ResultVO.failure("类别出错");
        }
    }
    @PutMapping("update")
    public ResultVO update(@RequestBody Owner owner){
        ownerService.updateByPrimaryKeySelective(owner);
        return ResultVO.success();
    }
}
